﻿using System;
using System.Reflection;
using System.Diagnostics;

namespace PerformanceDude.MSBuildShellExtension
{
    [DebuggerDisplay("{ProjectName}, {RelativePath}, {ProjectGuid}")]
    public class SolutionProject
    {
        static readonly Type _ProjectInSolution;
        static readonly PropertyInfo _ProjectName;
        static readonly PropertyInfo _RelativePath;
        static readonly PropertyInfo _ProjectGuid;

        static SolutionProject()
        {
            _ProjectInSolution = Type.GetType("Microsoft.Build.Construction.ProjectInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
            if (_ProjectInSolution != null)
            {
                _ProjectName = _ProjectInSolution.GetProperty("ProjectName", BindingFlags.NonPublic | BindingFlags.Instance);
                _RelativePath = _ProjectInSolution.GetProperty("RelativePath", BindingFlags.NonPublic | BindingFlags.Instance);
                _ProjectGuid = _ProjectInSolution.GetProperty("ProjectGuid", BindingFlags.NonPublic | BindingFlags.Instance);
            }
        }

        public string ProjectName { get; private set; }
        public string RelativePath { get; private set; }
        public string ProjectGuid { get; private set; }

        public SolutionProject(object solutionProject)
        {
            this.ProjectName = _ProjectName.GetValue(solutionProject, null) as string;
            this.RelativePath = _RelativePath.GetValue(solutionProject, null) as string;
            this.ProjectGuid = _ProjectGuid.GetValue(solutionProject, null) as string;
        }
    }

}
